package com.huamao.zh.service.impl;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.huamao.zh.mapper.AccountMapper;
import com.huamao.zh.mapper.AccountShopMapper;
import com.huamao.zh.mapper.AgentsApplysMapper;
import com.huamao.zh.mapper.DateMapper;
import com.huamao.zh.mapper.LogCashFreezeMapper;
import com.huamao.zh.mapper.LogCashMapper;
import com.huamao.zh.mapper.LogKaiyuanFreezeMapper;
import com.huamao.zh.mapper.LogKaiyuanTurnoverMapper;
import com.huamao.zh.mapper.LogScoreFreezeMapper;
import com.huamao.zh.mapper.LogScoreMapper;
import com.huamao.zh.mapper.LogScoreSpecialMapper;
import com.huamao.zh.mapper.SysConfigsMapper;
import com.huamao.zh.mapper.UsersMapper;
import com.huamao.zh.mapper.UsersUpgradeMapper;
import com.huamao.zh.model.Account;
import com.huamao.zh.model.AccountShop;
import com.huamao.zh.model.AgentsApplys;
import com.huamao.zh.model.LogCash;
import com.huamao.zh.model.LogCashFreeze;
import com.huamao.zh.model.LogKaiyuanFreeze;
import com.huamao.zh.model.LogKaiyuanTurnover;
import com.huamao.zh.model.LogScore;
import com.huamao.zh.model.LogScoreFreeze;
import com.huamao.zh.model.LogScoreSpecial;
import com.huamao.zh.model.SKDate;
import com.huamao.zh.model.Users;
import com.huamao.zh.model.UsersUpgrade;
import com.huamao.zh.service.FenrunService;
import com.huamao.zh.service.UserIdentityUpgradeService;

@Service
public class UserIdentityUpgradeServiceImpl implements UserIdentityUpgradeService {
	@Autowired
	private UsersUpgradeMapper usersUpgradeMapper;

	@Autowired
	private UsersMapper usersMapper;

	@Autowired
	private AccountMapper accountMapper;

	@Autowired
	private LogCashFreezeMapper logCashFreezeMapper;

	@Autowired
	private LogCashMapper logCashMapper;

	@Autowired
	private LogScoreFreezeMapper logScoreFreezeMapper;

	@Autowired
	private LogScoreMapper logScoreMapper;

	@Autowired
	private AccountShopMapper accountShopMapper;

	@Autowired
	private LogKaiyuanFreezeMapper logKaiyuanFreezeMapper;

	@Autowired
	private LogKaiyuanTurnoverMapper logKaiyuanTurnoverMapper;

	@Autowired
	private SysConfigsMapper sysConfigsMapper;

	@Autowired
	private LogScoreSpecialMapper logScoreSpecialMapper;

	@Autowired
	private AgentsApplysMapper agentsApplysMapper;

	@Autowired
	private FenrunService fenrunService;

	@Autowired
	private DateMapper dateMapper;

	/**
	 * 功能说明：用户升级 Author：lsx SKDate:2017-11-23
	 */

	@Override
	public void usersUpgrade() {
		synchronized (this.getClass()) {

			String remark = "用户升级";
			// LogUtil.info(LogCodeType.CODE_6000, remark + "开始");
			try {
				Map<String, Object> map = new HashMap<>();
				map.put("status", 1); // 未处理

				Integer upgradeListCount = usersUpgradeMapper.queryListCount(map);
				// LogUtil.info(LogCodeType.CODE_6000,
				// remark + " 升级用户的个数 : upgradeListCount: " +
				// JSON.toJSONString(upgradeListCount));

				for (Integer i = 0; i < upgradeListCount; i += 100) { // 每次遍历100个
					map.put("start", 0);
					map.put("offset", 100);
					List<UsersUpgrade> upgradeList = usersUpgradeMapper.queryListByStatusAndDate(map);
					if (upgradeList.isEmpty() || upgradeList.size() == 0) {
						return;
					}
					// LogUtil.info(LogCodeType.CODE_6000,
					// remark + " 升级用户的集合 :upgradeList: " +
					// JSON.toJSONString(upgradeList));

					Date date = new Date();

					for (UsersUpgrade usersUpgrade : upgradeList) {
						// 普通用户升级为主管
						if (usersUpgrade.getPreRole() == 0 && usersUpgrade.getAfterRole() == 1)
							this.commonUsersUpgradeDirector(usersUpgrade, date);
						// 主管升级为经理
						if (usersUpgrade.getPreRole() == 1 && usersUpgrade.getAfterRole() == 2) {
							directorUsersUpgradeManager(usersUpgrade, date);
						}
						// 普通用户升级为经理
						if (usersUpgrade.getPreRole() == 0 && usersUpgrade.getAfterRole() == 2) {
							commonUsersUpgradeManager(usersUpgrade, date);
						}
					}
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
			// LogUtil.info(LogCodeType.CODE_6000, remark + "结束");
		}
	}

	/**
	 * 功能说明：普通用户升级为主管 Author：lsx SKDate:2017-11-23
	 *
	 * @param usersUpgrade
	 * @param date
	 * @throws Exception
	 */
	@Transactional
	public void commonUsersUpgradeDirector(UsersUpgrade usersUpgrade, Date date) {
		String remark = "普通用户升级为主管";
		// LogUtil.info(LogCodeType.CODE_6001, remark + "开始" + "usersUpgrade :"
		// + JSON.toJSONString(usersUpgrade)
		// + " date :" + JSON.toJSONString(date));
		try {
			// 升级用户为主管
			Users user = new Users();
			user.setUserId(usersUpgrade.getUserId());
			user.setUserType(1);
			// LogUtil.info(LogCodeType.CODE_6001, remark + " 改变用户状态 user ：" +
			// JSON.toJSONString(user));
			usersMapper.updateSelective(user);
			// 更新用户升级状态
			usersUpgrade.setStatus(3);
			usersUpgrade.setBatTime(date);
			// LogUtil.info(LogCodeType.CODE_6001, remark + " 更新用户升级状态
			// usersUpgrade ：" + JSON.toJSONString(usersUpgrade));
			usersUpgradeMapper.updateSelective(usersUpgrade);
			// 待发现金转到可用现金
			this.freezeCashToCash(usersUpgrade, date, remark);
			// 待发积分转到可用积分
			this.freezeScoreToScore(usersUpgrade, date, remark);
			// 待发华宝营业额转到华宝营业额
			this.freezeKaiYuanToKaiYuan(usersUpgrade, date, remark);
			// 对上线的发展人数加一
			this.addUsersinviteNum(usersUpgrade);
			// 划拨特别奖励
			this.specialReward(usersUpgrade, date, remark);
			// 分润奖励业务
			String fieldName = "directorProfit"; // 普通升级主管奖励分享人(元)
			this.fenRunReward(usersUpgrade, fieldName);

		} catch (Exception e) {
			e.printStackTrace();
		}

		// LogUtil.info(LogCodeType.CODE_6001, remark + "结束");

	}

	/**
	 * 功能说明：主管升级为经理 Author：lsx SKDate:2017-11-24
	 *
	 * @param usersUpgrade
	 * @param date
	 * @throws Exception
	 */
	@Transactional
	public void directorUsersUpgradeManager(UsersUpgrade usersUpgrade, Date date) {
		String remark = "主管升级为经理";
		// LogUtil.info(LogCodeType.CODE_6002, remark + "开始" + "usersUpgrade :"
		// + JSON.toJSONString(usersUpgrade)
		// + " date :" + JSON.toJSONString(date));
		try {
			// 主管升级为经理
			Users user = new Users();
			user.setUserId(usersUpgrade.getUserId());
			user.setUserType(2);
			// LogUtil.info(LogCodeType.CODE_6002, remark + " 改变用户状态 users ：" +
			// JSON.toJSONString(user));
			usersMapper.updateSelective(user);
			// 更新用户升级状态
			usersUpgrade.setStatus(3);
			usersUpgrade.setBatTime(date);
			// LogUtil.info(LogCodeType.CODE_6002, remark + " 更新用户升级状态
			// usersUpgrade ：" + JSON.toJSONString(usersUpgrade));
			usersUpgradeMapper.updateSelective(usersUpgrade);
			// 分润奖励业务
			String fieldName = "managerProfit"; // 主管升级经理奖励分享人(元)
			this.fenRunReward(usersUpgrade, fieldName);

			// 判断下线是否7天内升级为普通代理股东
			this.upgradeCommonShareholder(usersUpgrade, date, remark);

		} catch (Exception e) {
			e.printStackTrace();
		}
		// LogUtil.info(LogCodeType.CODE_6002, remark + "结束");

	}

	/**
	 * 功能说明：普通用户升级为经理 Author：lsx SKDate:2017-11-24
	 *
	 * @param date
	 * @param usersUpgrade
	 * @throws Exception
	 */
	@Transactional
	public void commonUsersUpgradeManager(UsersUpgrade usersUpgrade, Date date) {
		String remark = "普通用户升级为经理";
		// LogUtil.info(LogCodeType.CODE_6003, remark + "开始" + "usersUpgrade :"
		// + JSON.toJSONString(usersUpgrade)
		// + " date :" + JSON.toJSONString(date));
		try {
			Users user = new Users();
			user.setUserId(usersUpgrade.getUserId());
			user.setUserType(2);
			// LogUtil.info(LogCodeType.CODE_6003, remark + " 改变用户状态 users ：" +
			// JSON.toJSONString(user));
			usersMapper.updateSelective(user);
			// 更新用户升级状态
			usersUpgrade.setStatus(3);
			usersUpgrade.setBatTime(date);
			// LogUtil.info(LogCodeType.CODE_6003, remark + " 更新用户升级状态
			// usersUpgrade ：" + JSON.toJSONString(usersUpgrade));
			usersUpgradeMapper.updateSelective(usersUpgrade);
			// 待发现金转到可用现金
			this.freezeCashToCash(usersUpgrade, date, remark);
			// 待发积分转到可用积分
			this.freezeScoreToScore(usersUpgrade, date, remark);
			// 待发华宝营业额转到华宝营业额
			this.freezeKaiYuanToKaiYuan(usersUpgrade, date, remark);
			// 对上线的发展人数加一
			this.addUsersinviteNum(usersUpgrade);
			// 划拨特别奖励
			this.specialReward(usersUpgrade, date, remark);
			// 分润奖励业务
			String fieldName = "commonProfit"; // 普通升级经理奖励分享人(元)
			this.fenRunReward(usersUpgrade, fieldName);

			// 判断下线是否7天内升级为普通代理股东
			this.upgradeCommonShareholder(usersUpgrade, date, remark);

		} catch (Exception e) {
			e.printStackTrace();
		}
		// LogUtil.info(LogCodeType.CODE_6003, remark + "结束");
	}

	/**
	 * @param usersUpgrade,fieldName
	 * @throws Exception
	 * @desc 分润奖励业务
	 * @author lsx
	 */
	public void fenRunReward(UsersUpgrade usersUpgrade, String fieldName) throws Exception {
		// LogUtil.info(LogCodeType.CODE_6014, " 分润奖励业务开始 ：usersUpgrade : " +
		// JSON.toJSONString(usersUpgrade)
		// + "fieldName : " + JSON.toJSONString(fieldName));
		Map<String, Object> map = new HashMap<>();
		map.put("source", "C");
		map.put("userId", usersUpgrade.getUserId());
		map.put("shopUserId", 0);
		map.put("orderNo", usersUpgrade.getOrderNo());
		String fieldValue = sysConfigsMapper.getByFieldCode(fieldName).getFieldValue();
		map.put("totalMoney", new BigDecimal(fieldValue));

		map.put("totalScore", usersUpgrade.getTotalMoney().multiply(new BigDecimal(100)));// 例子
																							// ：997*100
		// LogUtil.info(LogCodeType.CODE_6014, " 分润奖励业务参数 ： map : " +
		// JSON.toJSONString(map));
		// fenrunService.doFenRun(JSON.toJSONString(map));

		// LogUtil.info(LogCodeType.CODE_6014, " 分润奖励业务结束");

	}

	/**
	 * @param usersUpgrade
	 * @param date
	 * @param remark
	 * @throws Exception
	 * @desc 判断下线是否7个工作日内升级为普通代理股东
	 * @author lsx
	 */
	public void upgradeCommonShareholder(UsersUpgrade usersUpgrade, Date date, String remark) throws Exception {
		// LogUtil.info(LogCodeType.CODE_6013,
		// " 判断下线是否7个工作日内升级为普通代理股东开始 ：usersUpgrade : " +
		// JSON.toJSONString(usersUpgrade) + "date : "
		// + JSON.toJSONString(date) + "remark : " + JSON.toJSONString(remark));

		Integer userId = usersUpgrade.getUserId();
		// LogUtil.info(LogCodeType.CODE_6013, " userId " +
		// JSON.toJSONString(userId));

		List<Users> users = usersMapper.getByInviteId(userId);
		// 下线是否为空
		if (users.size() == 0 || users.isEmpty()) {
			return;
		}
		Users upUsers = usersMapper.get(userId);
		Integer isAgent = upUsers.getIsAgent();
		// LogUtil.info(LogCodeType.CODE_6013, "isAgent : " + isAgent);
		if (0 == isAgent) {
			String newremark = remark + "，下线升级奖励";
			// LogUtil.info(LogCodeType.CODE_6013, " 下线升级奖励");
			reward(users, usersUpgrade, userId, newremark, date);
			// LogUtil.info(LogCodeType.CODE_6013, " 下线升级奖励");
		}
		// LogUtil.info(LogCodeType.CODE_6013, " 判断下线是否7个工作日内升级为普通代理股东结束");
	}

	public void reward(List<Users> users, UsersUpgrade usersUpgrade, Integer userId, String remark, Date date)
			throws Exception {
		for (Users User : users) {
			Date upgradeTime = null;
			Date twoCheckTime = null;
			AgentsApplys agentsApplys = null;
			try {
				// type = 7; 普通股东
				// status = 3; 二审完结
				agentsApplys = agentsApplysMapper.getAgentsApplysByUserId(User.getUserId());
				upgradeTime = usersUpgrade.getUpgradeTime();
				twoCheckTime = agentsApplys.getTwoCheckTime();
			} catch (Exception e) {

			}
			if (null == twoCheckTime || null == upgradeTime) {
				continue;
			}
			// LogUtil.info(LogCodeType.CODE_6013, " 升级时间 ：upgradeTime " +
			// upgradeTime);
			// LogUtil.info(LogCodeType.CODE_6013, " 二审时间 ：twoCheckTime " +
			// twoCheckTime);

			if (isSevenWorkingDaysBefore(upgradeTime, twoCheckTime)) {
				BigDecimal money = new BigDecimal(0);
				try {
					money = agentsApplys.getMoney(); // 缴纳代理费用
				} catch (Exception e) {
					e.printStackTrace();
				}
				if (null == money || "null".equals(money.toString())) {
					continue;
				}
				// LogUtil.info(LogCodeType.CODE_6013, " 缴纳代理费用 ：money " +
				// JSON.toJSONString(money));
				BigDecimal rewardScore = money.multiply(new BigDecimal(200)); // 奖励积分
				// LogUtil.info(LogCodeType.CODE_6013, " 奖励积分 ：rewardScore " +
				// JSON.toJSONString(rewardScore));
				BigDecimal preScore = accountMapper.getByUserId(userId).getScore(); // 之前积分
				// LogUtil.info(LogCodeType.CODE_6013, " 之前积分 ：preScore " +
				// JSON.toJSONString(preScore));
				// 计入积分
				Map<String, Object> map = new HashMap<>();
				map.put("userId", userId);
				map.put("rewardScore", rewardScore);
				// LogUtil.info(LogCodeType.CODE_6013, " 计入积分 ： " +
				// JSON.toJSONString(map));
				accountMapper.updateScoreByUserId(map);
				// 积分流水
				LogScore logScore = new LogScore();
				logScore.setType(10); // 下线升级代理奖励
				logScore.setFromId(User.getUserId());
				logScore.setUserId(userId);
				logScore.setOrderNo(usersUpgrade.getOrderNo());
				logScore.setPreScore(preScore);
				logScore.setScoreType(1);
				logScore.setScore(rewardScore);
				logScore.setRemark(remark);
				logScore.setDataFlag(1);
				logScore.setCreateTime(date);
				logScoreMapper.add(logScore);
				// LogUtil.info(LogCodeType.CODE_6013, " 积分流水 ：logScore " +
				// JSON.toJSONString(logScore));
			}
		}

	}

	/**
	 * @param upgradeTime,twoCheckTime
	 * @throws Exception
	 * @desc 判断是否7个工作日内
	 * @author lsx
	 */
	public Boolean isSevenWorkingDaysBefore(Date upgradeTime, Date twoCheckTime) {
		boolean flag = false;
		try {
			Calendar calendar = Calendar.getInstance(); // 得到日历
			calendar.setTime(twoCheckTime);// 把二审时间赋给日历
			int sevenWorkingDays = 0;

			SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");
			String DateFormat = simpleDateFormat.format(calendar.getTime());

			List<SKDate> skDates = dateMapper.queryList(null);
			SKDate skDate = null;
			for (SKDate SKDate : skDates) {
				if (DateFormat.equals(SKDate.getDate().toString())) {
					skDate = SKDate;
				}
			}

			List<SKDate> SkDatesChliden = skDates.subList(skDates.indexOf(skDate), skDates.size());

			for (SKDate skDateChliden : SkDatesChliden) {
				if (skDateChliden.getStatus() == 0) {
					sevenWorkingDays = sevenWorkingDays + 1;
				}
				if (sevenWorkingDays == 7) {
					skDate = skDateChliden;
					break;
				}
			}

			List<SKDate> SkDatesList = SkDatesChliden.subList(SkDatesChliden.indexOf(skDate), SkDatesChliden.size());

			skDate = SkDatesList.get(0);
			String stringDate = skDate.getDate().toString();
			String year = stringDate.substring(0, 4);
			String month = stringDate.substring(4, 6);
			String day = stringDate.substring(6, 8);

			SimpleDateFormat simpleDate = new SimpleDateFormat("yyyy-MM-dd");
			Date sevenWorkingDate = simpleDate.parse(year + "-" + month + "-" + day);

			if (upgradeTime.before(sevenWorkingDate)) {
				flag = true;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return flag;

	}

	/**
	 * @param usersUpgrade,date,remark
	 * @desc 划拨特别奖励
	 * @author lsx
	 */
	public void specialReward(UsersUpgrade usersUpgrade, Date date, String remark) {
		// LogUtil.info(LogCodeType.CODE_6012, " 划拨特别奖励开始 ：usersUpgrade : " +
		// JSON.toJSONString(usersUpgrade) + "date : "
		// + JSON.toJSONString(date) + "remark : " + JSON.toJSONString(remark));

		Integer inviteId = usersMapper.get(usersUpgrade.getUserId()).getInviteId(); // 用户上线

		// LogUtil.info(LogCodeType.CODE_6012, " 用户上线 ：inviteId : " +
		// JSON.toJSONString(inviteId));

		if (inviteId == null || inviteId == 0 || "0".equals(inviteId)) {
			return;
		}
		Users users = usersMapper.get(inviteId);
		if (users == null) {
			return;
		}
		Integer inviteNum = accountMapper.getUsersinviteNum(inviteId); // 用户发展的主管或经理数量
		BigDecimal PreScore = accountMapper.getByUsersId(inviteId).getScore();
		// 划拨前积分
		// LogUtil.info(LogCodeType.CODE_6012, " 用户发展的主管或经理数量 ：inviteNum : " +
		// JSON.toJSONString(inviteNum));
		// LogUtil.info(LogCodeType.CODE_6012, " 划拨前积分 ：PreScore : " +
		// JSON.toJSONString(PreScore));
		BigDecimal score = null; // 划拨积分
		switch (inviteNum) {
		case 1:
			score = accountMapper.getByUsersId(inviteId).getOne();
			// LogUtil.info(LogCodeType.CODE_6012, " 划拨积分 ：score : " +
			// JSON.toJSONString(score));
			if (users.getUserType() == 0) {
				accountMapper.updateOneToFreezeScore(inviteId);
			} else {
				accountMapper.updateOneToScore(inviteId);
			}
			this.specialRewardLogScore(users, usersUpgrade, inviteId, PreScore, score, date, remark);
			break;
		case 2:
			score = accountMapper.getByUsersId(inviteId).getTwo();
			// LogUtil.info(LogCodeType.CODE_6012, " 划拨积分 ：score : " +
			// JSON.toJSONString(score));
			if (users.getUserType() == 0) {
				accountMapper.updateTwoToFreezeScore(inviteId);
			} else {
				accountMapper.updateTwoToScore(inviteId);
			}
			this.specialRewardLogScore(users, usersUpgrade, inviteId, PreScore, score, date, remark);
			break;
		case 3:
			score = accountMapper.getByUsersId(inviteId).getThree();
			// LogUtil.info(LogCodeType.CODE_6012, " 划拨积分 ：score : " +
			// JSON.toJSONString(score));
			if (users.getUserType() == 0) {
				accountMapper.updateThreeToFreezeScore(inviteId);
			} else {
				accountMapper.updateThreeToScore(inviteId);
			}
			this.specialRewardLogScore(users, usersUpgrade, inviteId, PreScore, score, date, remark);
			break;
		case 4:
			score = accountMapper.getByUsersId(inviteId).getFour();
			// LogUtil.info(LogCodeType.CODE_6012, " 划拨积分 ：score : " +
			// JSON.toJSONString(score));
			if (users.getUserType() == 0) {
				accountMapper.updateFourToFreezeScore(inviteId);
			} else {
				accountMapper.updateFourToScore(inviteId);
			}
			this.specialRewardLogScore(users, usersUpgrade, inviteId, PreScore, score, date, remark);
			break;
		case 5:
			score = accountMapper.getByUsersId(inviteId).getFive();
			// LogUtil.info(LogCodeType.CODE_6012, " 划拨积分 ：score : " +
			// JSON.toJSONString(score));
			if (users.getUserType() == 0) {
				accountMapper.updateFiveToFreezeScore(inviteId);
			} else {
				accountMapper.updateFiveToScore(inviteId);
			}
			this.specialRewardLogScore(users, usersUpgrade, inviteId, PreScore, score, date, remark);
			break;
		default:
			break;
		}

	}

	/**
	 * @param usersUpgrade,inviteId,PreScore,score,date,remark
	 * @desc 划拨特别奖励积分流水
	 * @author lsx
	 */
	public void specialRewardLogScore(Users users, UsersUpgrade usersUpgrade, Integer inviteId, BigDecimal PreScore,
			BigDecimal score, Date date, String remark) {
		// LogUtil.info(LogCodeType.CODE_6011,
		// " 划拨特别奖励积分流水开始 ：usersUpgrade : " + JSON.toJSONString(usersUpgrade) +
		// " users : "
		// + JSON.toJSONString(users) + "PreScore : " +
		// JSON.toJSONString(PreScore) + "score : "
		// + JSON.toJSONString(score) + "inviteId : " +
		// JSON.toJSONString(inviteId) + "date : "
		// + JSON.toJSONString(date) + "remark : " + JSON.toJSONString(remark));
		if (Double.parseDouble(score.toString()) < 0.001) {
			return;
		}
		// 转到正式账号的特别奖励流水
		LogScoreSpecial logScoreSpecial = new LogScoreSpecial();
		logScoreSpecial.setType(31);
		logScoreSpecial.setFromId(inviteId);
		logScoreSpecial.setUserId(inviteId);
		logScoreSpecial.setOrderNo(usersUpgrade.getOrderNo());
		logScoreSpecial.setPreScore(PreScore);
		logScoreSpecial.setScoreType(-1);
		logScoreSpecial.setScore(score);
		if (users.getUserType() == 0) {
			logScoreSpecial.setRemark(remark + "，划拨特别奖励,转到待发账号的特别奖励");
		} else {
			logScoreSpecial.setRemark(remark + "，划拨特别奖励,转到正式账号的特别奖励");
		}
		logScoreSpecial.setDataFlag(1);
		logScoreSpecial.setCreateTime(date);
		logScoreSpecialMapper.add(logScoreSpecial);
		// LogUtil.info(LogCodeType.CODE_6011,
		// "划拨特别奖励积分流水 : 转到正式账号的特别奖励流水: logScoreSpecial ： " +
		// JSON.toJSONString(logScoreSpecial));
		if (users.getUserType() == 0) {
			// 转待发积分流水
			LogScoreFreeze logScoreFreeze = new LogScoreFreeze();
			logScoreFreeze.setType(8);
			logScoreFreeze.setFromId(inviteId);
			logScoreFreeze.setUserId(inviteId);
			logScoreFreeze.setOrderNo(usersUpgrade.getOrderNo());
			logScoreFreeze.setPreScore(PreScore);
			logScoreFreeze.setScoreType(1);
			logScoreFreeze.setScore(score);
			logScoreFreeze.setRemark(remark + ",划拨特别奖励,待发积分流水");
			logScoreFreeze.setDataFlag(1);
			logScoreFreeze.setCreateTime(date);
			logScoreFreezeMapper.add(logScoreFreeze);
			// LogUtil.info(LogCodeType.CODE_6011,
			// "划拨特别奖励待发积分流水 : 待发积分流水: logScoreFreeze ： " +
			// JSON.toJSONString(logScoreFreeze));
		} else {
			// 转正式积分流水
			LogScore logScore = new LogScore();
			logScore.setType(15);
			logScore.setFromId(inviteId);
			logScore.setUserId(inviteId);
			logScore.setOrderNo(usersUpgrade.getOrderNo());
			logScore.setPreScore(PreScore);
			logScore.setScoreType(1);
			logScore.setScore(score);
			logScore.setRemark(remark + ",划拨特别奖励,积分流水");
			logScore.setDataFlag(1);
			logScore.setCreateTime(date);
			logScoreMapper.add(logScore);
			// LogUtil.info(LogCodeType.CODE_6011, "划拨特别奖励积分流水 : 积分流水: logScore
			// ： " + JSON.toJSONString(logScore));
		}
		// LogUtil.info(LogCodeType.CODE_6011, "划拨特别奖励积分流水结束");
	}

	/**
	 * @param usersUpgrade
	 * @desc 对上线的发展人数加一
	 * @author lsx
	 */
	public void addUsersinviteNum(UsersUpgrade usersUpgrade) {
		// LogUtil.info(LogCodeType.CODE_6010, " 对上线的发展人数加一开始 ： usersUpgrade ： "
		// + JSON.toJSONString(usersUpgrade));
		Integer inviteId = usersMapper.get(usersUpgrade.getUserId()).getInviteId(); // 用户上线
		if (inviteId == null || inviteId == 0 || "0".equals(inviteId)) {
			return;
		}
		// LogUtil.info(LogCodeType.CODE_6010, " 上线ID inviteId ： " +
		// JSON.toJSONString(inviteId));
		accountMapper.addSelectiveUsersinviteNum(inviteId);

		// LogUtil.info(LogCodeType.CODE_6010, " 对上线的发展人数加一 结束 ");
	}

	/**
	 * @param usersUpgrade
	 * @param date
	 * @param remark
	 * @desc 待发华宝营业额转到华宝营业额
	 * @author lsx
	 */
	public void freezeKaiYuanToKaiYuan(UsersUpgrade usersUpgrade, Date date, String remark) {
		// LogUtil.info(LogCodeType.CODE_6009, " 待发华宝营业额转到华宝营业额开始 ：usersUpgrade
		// : " + JSON.toJSONString(usersUpgrade)
		// + "date : " + JSON.toJSONString(date) + "remark : " +
		// JSON.toJSONString(remark));

		AccountShop accountShop = accountShopMapper.getByUsersId(usersUpgrade.getUserId());
		// LogUtil.info(LogCodeType.CODE_6009, " accountShop ： " +
		// JSON.toJSONString(accountShop));
		if (accountShop == null) {
			return;
		}
		BigDecimal preKaiyuan = accountShop.getKaiyuanTurnover();
		BigDecimal kaiyuan = accountShop.getFreezeKaiyuan();
		if (Double.parseDouble(kaiyuan.toString()) < 0.001) {
			return;
		}
		// LogUtil.info(LogCodeType.CODE_6009, " preKaiyuan ： " +
		// JSON.toJSONString(preKaiyuan));
		// LogUtil.info(LogCodeType.CODE_6009, " kaiyuan ： " +
		// JSON.toJSONString(kaiyuan));

		accountShopMapper.updateKaiYuan(usersUpgrade.getUserId());
		// LogUtil.info(LogCodeType.CODE_6009, " accountShop ： " +
		// JSON.toJSONString(accountShop));

		// 待发华宝营业额流水
		LogKaiyuanFreeze logKaiyuanFreeze = new LogKaiyuanFreeze();
		logKaiyuanFreeze.setType(31);
		logKaiyuanFreeze.setFromId(usersUpgrade.getUserId());
		logKaiyuanFreeze.setUserId(usersUpgrade.getUserId());
		logKaiyuanFreeze.setOrderNo(usersUpgrade.getOrderNo());
		logKaiyuanFreeze.setPreKaiyuan(kaiyuan);
		logKaiyuanFreeze.setKaiyuanType(-1);
		logKaiyuanFreeze.setKaiyuan(kaiyuan);
		logKaiyuanFreeze.setRemark(remark + "，待发华宝营业额转为可用华宝营业额");
		logKaiyuanFreeze.setDataFlag(1);
		logKaiyuanFreeze.setCreateTime(date);
		logKaiyuanFreezeMapper.add(logKaiyuanFreeze);
		// LogUtil.info(LogCodeType.CODE_6009,
		// "待发华宝营业额转为可用华宝营业额 :待发华宝营业额流水: logKaiyuanFreeze ： " +
		// JSON.toJSONString(logKaiyuanFreeze));

		// 华宝营业额流水
		LogKaiyuanTurnover logKaiyuanTurnover = new LogKaiyuanTurnover();
		logKaiyuanTurnover.setType(4);
		logKaiyuanTurnover.setFromId(usersUpgrade.getUserId());
		logKaiyuanTurnover.setUserId(usersUpgrade.getUserId());
		logKaiyuanTurnover.setOrderNo(usersUpgrade.getOrderNo());
		logKaiyuanTurnover.setPreKaiyuan(preKaiyuan);
		logKaiyuanTurnover.setKaiyuanType(1);
		logKaiyuanTurnover.setKaiyuan(kaiyuan);
		logKaiyuanTurnover.setRemark(remark + "，待发华宝营业额转为可用华宝营业额");
		logKaiyuanTurnover.setDataFlag(1);
		logKaiyuanTurnover.setCreateTime(date);
		logKaiyuanTurnoverMapper.add(logKaiyuanTurnover);
		// LogUtil.info(LogCodeType.CODE_6009,
		// "待发华宝营业额转为可用华宝营业额 :华宝营业额流水: logKaiyuanTurnover ： " +
		// JSON.toJSONString(logKaiyuanTurnover));
		// LogUtil.info(LogCodeType.CODE_6009, "待发华宝营业额转为可用华宝营业额 结束");

	}

	/**
	 * @param usersUpgrade
	 * @param date
	 * @param remark
	 * @desc 待发积分转到可用积分
	 * @author lsx
	 */
	public void freezeScoreToScore(UsersUpgrade usersUpgrade, Date date, String remark) {
		// LogUtil.info(LogCodeType.CODE_6008, " 待发积分转到可用积分开始 ：usersUpgrade : "
		// + JSON.toJSONString(usersUpgrade)
		// + "date : " + JSON.toJSONString(date) + "remark : " +
		// JSON.toJSONString(remark));
		Account account = accountMapper.getByUsersId(usersUpgrade.getUserId());
		// LogUtil.info(LogCodeType.CODE_6008, " account ： " +
		// JSON.toJSONString(account));
		BigDecimal preScore = account.getScore();
		BigDecimal score = account.getFreezeScore();
		if (Double.parseDouble(score.toString()) < 0.001) {
			return;
		}
		// LogUtil.info(LogCodeType.CODE_6008, " preScore ： " +
		// JSON.toJSONString(preScore));
		// LogUtil.info(LogCodeType.CODE_6008, " score ： " +
		// JSON.toJSONString(score));
		accountMapper.updateScore(usersUpgrade.getUserId());
		// LogUtil.info(LogCodeType.CODE_6008, " account ： " +
		// JSON.toJSONString(account));

		// 待发积分流水
		LogScoreFreeze logScoreFreeze = new LogScoreFreeze();
		logScoreFreeze.setType(31);
		logScoreFreeze.setFromId(usersUpgrade.getUserId());
		logScoreFreeze.setUserId(usersUpgrade.getUserId());
		logScoreFreeze.setOrderNo(usersUpgrade.getOrderNo());
		logScoreFreeze.setPreScore(score);
		logScoreFreeze.setScoreType(-1);
		logScoreFreeze.setScore(score);
		logScoreFreeze.setRemark(remark + "，待发积分转为可用积分");
		logScoreFreeze.setDataFlag(1);
		logScoreFreeze.setCreateTime(date);
		logScoreFreezeMapper.add(logScoreFreeze);
		// LogUtil.info(LogCodeType.CODE_6008, " 待发积分转为可用积分 ：待发积分流水
		// logScoreFreeze ：" + JSON.toJSONString(logScoreFreeze));

		// 积分流水
		LogScore logScore = new LogScore();
		logScore.setType(12);
		logScore.setFromId(usersUpgrade.getUserId());
		logScore.setUserId(usersUpgrade.getUserId());
		logScore.setOrderNo(usersUpgrade.getOrderNo());
		logScore.setPreScore(preScore);
		logScore.setScoreType(1);
		logScore.setScore(score);
		logScore.setRemark(remark + "，待发积分转为可用积分");
		logScore.setDataFlag(1);
		logScore.setCreateTime(date);
		logScoreMapper.add(logScore);
		// LogUtil.info(LogCodeType.CODE_6008, " 待发积分转为可用积分 ： 积分流水 logScore ：" +
		// JSON.toJSONString(logScore));
		// LogUtil.info(LogCodeType.CODE_6008, " 待发积分转为可用积分结束");

	}

	/**
	 * @param usersUpgrade
	 * @param date
	 * @desc 待发现金转到可用现金
	 * @author lsx
	 * @para remark
	 */
	public void freezeCashToCash(UsersUpgrade usersUpgrade, Date date, String remark) {
		// LogUtil.info(LogCodeType.CODE_6007, " 待发现金转到可用现金开始 ：usersUpgrade : "
		// + JSON.toJSONString(usersUpgrade)
		// + "date : " + JSON.toJSONString(date) + "remark : " +
		// JSON.toJSONString(remark));
		Account account = accountMapper.getByUsersId(usersUpgrade.getUserId());
		// LogUtil.info(LogCodeType.CODE_6007, " account ： " +
		// JSON.toJSONString(account));
		BigDecimal preCash = account.getCash();
		BigDecimal cash = account.getFreezeCash();
		if (Double.parseDouble(cash.toString()) < 0.001) {
			return;
		}
		// LogUtil.info(LogCodeType.CODE_6007, " preCash ： " +
		// JSON.toJSONString(preCash));
		// LogUtil.info(LogCodeType.CODE_6007, " cash ： " +
		// JSON.toJSONString(cash));
		accountMapper.updateCash(usersUpgrade.getUserId());
		// LogUtil.info(LogCodeType.CODE_6007, " account ： " +
		// JSON.toJSONString(account));
		// 待发现金流水
		LogCashFreeze logCashFreeze = new LogCashFreeze();
		logCashFreeze.setType(31);
		logCashFreeze.setFromId(usersUpgrade.getUserId());
		logCashFreeze.setUserId(usersUpgrade.getUserId());
		logCashFreeze.setOrderNo(usersUpgrade.getOrderNo());
		logCashFreeze.setPreCash(cash);
		logCashFreeze.setCashType(-1);
		logCashFreeze.setCash(cash);
		logCashFreeze.setRemark(remark + "，待发现金转为可用现金");
		logCashFreeze.setDataFlag(1);
		logCashFreeze.setCreateTime(date);
		logCashFreezeMapper.add(logCashFreeze);
		// LogUtil.info(LogCodeType.CODE_6007, " 待发现金转到可用现金 ： 待发现金流水
		// logCashFreeze ：" + JSON.toJSONString(logCashFreeze));
		// 现金流水
		LogCash logCash = new LogCash();
		logCash.setType(4);
		logCash.setFromId(usersUpgrade.getUserId());
		logCash.setUserId(usersUpgrade.getUserId());
		logCash.setOrderNo(usersUpgrade.getOrderNo());
		logCash.setPreCash(preCash);
		logCash.setCashType(1);
		logCash.setCash(cash);
		logCash.setRemark(remark + "，待发现金转为可用现金");
		logCash.setDataFlag(1);
		logCash.setCreateTime(date);
		logCashMapper.add(logCash);
		// LogUtil.info(LogCodeType.CODE_6007, " 待发现金转到可用现金 ： 现金流水 logCash ：" +
		// JSON.toJSONString(logCash));
		// LogUtil.info(LogCodeType.CODE_6007, " 待发现金转到可用现金结束 ");

	}

}
